<template>
  <div v-show="list.length">
    <div class="list-control">
      <div class="list-control-filter">
        <span>品牌：</span>
        <span
          class="list-control-filter-item"
          :class="{ on: filterBrand[index] == item }"
          v-for="(item, index) in brands"
          :key="index"
          @click="handleFilterBrand(item, index)"
        >{{ item }}</span>
      </div>
      <div class="list-control-filter">
        <span>颜色：</span>
        <span
          class="list-control-filter-item"
          :class="{ on: filterColor[index] == item }"
          v-for="(item, index) in colors"
          :key="index"
          @click="handleFilterColor(item, index)"
        >{{ item }}</span>
      </div>
      <div class="list-control-order">
        <span>排序：</span>
        <span
          class="list-control-order-item"
          :class="{ on: order === '' }"
          @click="handleOrderDefault"
        >默认</span>
        <span
          class="list-control-order-item"
          :class="{ on: order === 'sales' }"
          @click="handleOrderSales"
        >
          销量
          <template v-if="order === 'sales'">↓</template>
        </span>
        <span
          class="list-control-order-item"
          :class="{ on: order.indexOf('cost') > -1 }"
          @click="handleOrderCost"
        >
          价格
          <template v-if="order === 'cost-asc'">↑</template>
          <template v-if="order === 'cost-desc'">↓</template>
        </span>
      </div>
    </div>
    <Product v-for="item in filteredAndOrderedList" :info="item" :key="item.id"></Product>
    <div class="product-not-found" v-show="!filteredAndOrderedList.length">暂无相关商品</div>
  </div>
</template>

<script setup>
import { ref, computed, onMounted } from "vue";
import Product from "../components/product.vue";
import product_data from "../product";
import { useCounterStore } from "../pinia/counter";

const store = useCounterStore();

const list = computed(() => product_data);

const brands = computed(() => store.brands);

const colors = computed(() => store.colors);

const filteredAndOrderedList = computed(() => {
  let filteredList = [...list.value];
  if (filterBrand.value.length !== 0) {
    filteredList = filteredList.filter(
      item => filterBrand.value.indexOf(item.brand) > -1
    );
  }
  if (filterColor.value.length !== 0) {
    filteredList = filteredList.filter(
      item => filterColor.value.indexOf(item.color) > -1
    );
  }
  if (order.value !== "") {
    if (order.value === "sales") {
      filteredList = filteredList.sort((a, b) => b.sales - a.sales);
    } else if (order.value === "cost-desc") {
      filteredList = filteredList.sort((a, b) => b.cost - a.cost);
    } else if (order.value === "cost-asc") {
      filteredList = filteredList.sort((a, b) => a.cost - b.cost);
    }
  }
  return filteredList;
});

const filterBrand = ref([]);
const filterColor = ref([]);
const order = ref("");

const handleFilterBrand = (brand, index) => {
  if (filterBrand.value[index] === brand) {
    filterBrand.value[index] = "";
  } else {
    filterBrand.value[index] = brand;
  }
};

const handleFilterColor = (color, index) => {
  if (filterColor.value[index] === color) {
    filterColor.value[index] = "";
  } else {
    filterColor.value[index] = color;
  }
};

const handleOrderDefault = () => {
  order.value = "";
};

const handleOrderSales = () => {
  order.value = "sales";
};

const handleOrderCost = () => {
  if (order.value === "cost-desc") {
    order.value = "cost-asc";
  } else {
    order.value = "cost-desc";
  }
};

onMounted(() => {
  store.getProductList();
});
</script>
<style scoped>
.list-control {
  background: #fff;
  border-radius: 6px;
  margin: 16px;
  padding: 16px;
  box-shadow: 0 1px 1px rgba(0, 0, 0, 0.2);
}
.list-control-filter {
  margin-bottom: 16px;
}
.list-control-filter-item,
.list-control-order-item {
  cursor: pointer;
  display: inline-block;
  border: 1px solid #e9eaec;
  border-radius: 4px;
  margin-right: 6px;
  padding: 2px 6px;
}
.list-control-filter-item.on,
.list-control-order-item.on {
  background: #f2352e;
  border: 1px solid #f2352e;
  color: #fff;
}
.product-not-found {
  text-align: center;
  padding: 32px;
}
</style>